<script setup>
import { useRouter } from 'vue-router'
import { useAppStore } from '@/store/modules/app'
import { useUserStore } from '@/store/modules/user'
import { UserFilled } from '@element-plus/icons-vue'
import Hamburger from '../Hamburger/index.vue'
import Breadcrumb from '../Breadcrumb/index.vue'

const router = useRouter()
const appStore = useAppStore()
const userStore = useUserStore()

const toggleSidebar = () => {
  appStore.toggleSidebar(false)
}

const logout = () => {
  userStore.logout()
  router.push('/login')
}
</script>

<template>
  <div class="navigation-bar">
    <Breadcrumb class="breadcrumb" />
    <div class="right-menu">
      <el-dropdown class="right-menu-item">
        <div class="right-menu-avatar">
          <el-avatar v-if="userStore.headimgurl" :size="20">
            <el-image :src="userStore.headimgurl" />
          </el-avatar>
          <el-avatar v-else :icon="UserFilled" :size="20" />
          <span>{{ userStore.username }}</span>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="logout">
              <span style="display: block">退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.navigation-bar {
  display: flex;
  justify-content: space-between;
  height: 60px;
  padding-left: 20px;
  overflow: hidden;
  color: var(--el-color-white);

  .hamburger {
    display: flex;
    align-items: center;
    height: 100%;
    padding: 0 15px;
    cursor: pointer;
  }

  .breadcrumb {
    flex: 1;
    font-size: 16px;
    font-weight: bold;

    @media screen and (max-width: 576px) {
      display: none;
    }

    :deep(.no-redirect) {
      &:last-child {
        font-weight: bold;
        color: var(--el-color-black) !important;
      }
    }
  }

  .sidebar {
    flex: 1;
    min-width: 0;

    :deep(.el-menu) {
      background-color: transparent;
    }

    :deep(.el-sub-menu) {
      &.is-active {
        .el-sub-menu__title {
          color: var(--el-color-primary) !important;
        }
      }
    }
  }

  .right-menu {
    display: flex;
    align-items: center;
    height: 100%;
    margin-right: 10px;

    .right-menu-item {
      padding: 0 10px;
      cursor: pointer;

      .right-menu-avatar {
        display: flex;
        align-items: center;

        .el-avatar {
          margin-right: 10px;
        }

        span {
          font-size: 14px;
        }
      }
    }
  }
}
</style>
